<?php

use think\migration\Migrator;

class CouponOrderAddColumns extends Migrator
{
    public function up()
    {
        $sql = <<<SQL
        ALTER TABLE
            `coupon_order` ADD `account` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '充值账号',
            ADD `product_name` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '产品名称',
            ADD `product_image` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '产品图片';

        UPDATE
            coupon_order,
            goods_cart
        SET
            coupon_order.account = goods_cart.account,
            coupon_order.product_name = goods_cart.goods_name,
            coupon_order.product_image = goods_cart.image
        WHERE
            coupon_order.id = goods_cart.order_id;

        ALTER TABLE
            `coupon_order` CHANGE `agent_settle_status` `agent_settle_status` TINYINT NOT NULL DEFAULT '1' COMMENT '结算状态 1无需结算 2待结算 3已结算';

        UPDATE
            coupon_order
        SET
            agent_settle_status = 1
        WHERE
            agent_settle_status <> 3;
SQL;

        $this->execute($sql);
    }

    public function down()
    {
        $sql = <<<SQL
        ALTER TABLE `coupon_order`
            DROP `account`,
            DROP `product_name`,
            DROP `product_image`;
SQL;

        $this->execute($sql);
    }
}
